package org.jeecg.modules.wlhy.customer.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.wlhy.customer.dto.HyCustomerAdminDTO;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAdmin;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
* @Description: 业务管理员与货主的关系
*/
@Api(tags="货主管理-子账号信息")
@RestController
@RequestMapping("/customer/hyCustomerAdmin/")
@Slf4j
public class HyCustomerAdminController extends JeecgController<HyCustomerAdmin, IHyCustomerAdminService> {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IHyCustomerAdminService hyCustomerAdminService;

    /**
     * 业务管理员与货主的关系列表数据
     */
    @ApiOperation(value="货主管理-子账号信息-分页列表查询", notes="货主管理-子账号信息-分页列表查询")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Result<?> queryPageList(HyCustomerAdminDTO dto,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        Page<HyCustomerAdmin> page = new Page<>(pageNo, pageSize);
        return this.hyCustomerAdminService.getPage(page, dto);
    }


    @AutoLog(value = "业务管理员与货主的关系-添加")
    @ApiOperation(value="业务管理员与货主的关系-添加", notes="业务管理员与货主的关系-添加")
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public Result<?> add(@RequestBody HyCustomerAdminDTO dto) {

        if (StrUtil.isEmpty(dto.getCustomerId())) {
            return Result.error("未选择货主");
        }

        if (StrUtil.isEmpty(dto.getUserId())) {
            return Result.error("未选择业务管理");
        }

        return this.hyCustomerAdminService.add(dto);
    }


    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "业务管理员与货主的关系-通过id删除")
    @ApiOperation(value="业务管理员与货主的关系-通过id删除", notes="业务管理员与货主的关系-通过id删除")
    @DeleteMapping(value = "delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {

        if (0 == this.hyCustomerAdminService.count(new LambdaQueryWrapper<HyCustomerAdmin>().eq(HyCustomerAdmin::getId, id))) {
            return Result.error("该数据不存在");
        }

        boolean b = this.hyCustomerAdminService.removeById(id);
        if (!b) {
            return Result.error("删除失败");
        }

        return Result.OK("删除成功");
    }

}
